package com.jx.service.impl;

import com.jx.dao.DetailMapper;
import com.jx.dto.AttrDto;
import com.jx.dto.GoodDetailDto;
import com.jx.pojo.Attr;
import com.jx.pojo.Good;
import com.jx.pojo.GoodImage;
import com.jx.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private DetailMapper detailMapper;

    @Override
    public GoodDetailDto getGoodDetailById(Integer id) {
        // 1.获取商品的属性信息
        List<Attr> attrs = detailMapper.getAttrListById(id);
        List<AttrDto> attrDtos = attrs.stream().map(item -> {
            AttrDto attrDto = new AttrDto();
            attrDto.setAttrId(item.getAttrId());
            attrDto.setAttrName(item.getAttrName());
            attrDto.setAttrValueList(item.getAttrValueList().split(","));
            return attrDto;
        }).collect(Collectors.toList());
        System.out.println(attrDtos);
        // 2.获取商品的图片信息
        List<GoodImage> images = detailMapper.getGoodImageListById(id);
        // 3.获取商品信息
        Good good = detailMapper.getGoodDetailById(id);
        good.setGoodImageList(images);
        // 4.封装商品详情数据
        GoodDetailDto goodDetailDto = new GoodDetailDto();
        goodDetailDto.setPrice(good.getPrice());
        goodDetailDto.setAttrList(attrDtos);
        goodDetailDto.setGoodInfo(good);
        return goodDetailDto;
    }
}
